package org.imast.services;

import java.util.List;

import org.imast.utils.UtilHibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.imast.interfaces.InterfaceCustomer;
import org.imast.models.ModelCustomer;

public class ServiceCustomer extends UtilHibernate implements InterfaceCustomer{

	@Override
	public void save(ModelCustomer customer) {
		
		Session session = UtilHibernate.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		
		try {
			
			session.saveOrUpdate(customer);
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			session.getTransaction().rollback();
			e.printStackTrace();
			
		}
	}

	@Override
	public void delete(ModelCustomer customer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ModelCustomer find(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ModelCustomer> getAll() {
		
		Session session = UtilHibernate.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		List<ModelCustomer> clientes = null;
		
		try {
			
			clientes = (List<ModelCustomer>) session.createQuery("from ModelCustomer").list();
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			session.getTransaction().rollback();
			e.printStackTrace();
			
		}
		
		return clientes;
		
	}
	

}
